package com.wuzheng.wisgo.sanitation.cloud.controller.road;


import com.wuzheng.wisgo.sanitation.cloud.model.common.param.DeleteParam;
import com.wuzheng.wisgo.sanitation.cloud.model.job.param.JobRoadDetailParam;
import com.wuzheng.wisgo.sanitation.cloud.model.job.pojo.JobRoadDTO;
import com.wuzheng.wisgo.sanitation.cloud.model.job.param.JobRoadListParam;
import com.wuzheng.wisgo.sanitation.cloud.model.road.RoadStateDTO;
import com.wuzheng.wisgo.sanitation.cloud.repository.entity.person.User;
import com.wuzheng.wisgo.sanitation.cloud.service.job.JobRoadService;
import com.wuzheng.wisgo.sanitation.framework.base.annotation.Authorization;
import com.wuzheng.wisgo.sanitation.framework.base.annotation.CurrentUser;
import com.wuzheng.wisgo.sanitation.framework.core.constant.SysRespEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.wuzheng.wisgo.sanitation.framework.core.protocol.Result;

import java.util.List;


import org.springframework.web.bind.annotation.RestController;
import com.wuzheng.wisgo.sanitation.framework.base.controller.BaseController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author：hoipang
 * @date：Created in 2019-12-10
 * @description：作业道路控制器
 * @modified By：
 * @version: 1.0
 */
@RestController
@RequestMapping("/jobRoad")
@Api(value = "JobRoadCtrl", tags = "作业道路接口")
public class JobRoadCtrl extends BaseController {

    @Autowired
    JobRoadService jobRoadService;

    /**
     * 获取作业道路信息列表(支持分页)
     */
    @Authorization
    @PostMapping(value = "/list")
    @ApiOperation(value = "获取作业道路信息列表(支持分页)")
    public Result<List<JobRoadDTO>> list(@ApiIgnore @CurrentUser User user, @RequestBody JobRoadListParam jobRoadListParam) {
        return this.success(SysRespEnum.SUCCESS.getMessage(), jobRoadService.listJobRoadDTOs(user,jobRoadListParam));
    }

    /**
     * 查询地图上作业道路的状态
     */
    @PostMapping(value = "/states")
    @ApiOperation(value = "查询地图上作业道路的状态")
    public Result<List<RoadStateDTO>> list() {
        return this.success(SysRespEnum.SUCCESS.getMessage(), jobRoadService.listRoadStateDTOs());
    }

    /**
     * 删除作业道路
     */
    @Authorization
    @PostMapping(value = "/delete")
    @ApiOperation(value = "删除作业道路(支持批量)")
    public Result<String> delete(@RequestBody DeleteParam deleteParam) {
        jobRoadService.removeJobRoads(deleteParam.getIds());
        return this.success(SysRespEnum.SUCCESS.getMessage());
    }

    /**
     * 获取作业道路
     */
    @Authorization
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取作业道路")
    public Result<JobRoadDTO> get(@ApiIgnore @CurrentUser User user, @PathVariable(value = "id") String id) {
        return this.success(SysRespEnum.SUCCESS.getMessage(), jobRoadService.selectJobRoadDTO(user, id));
    }

    /**
     * 保存作业道路
     */
    @Authorization
    @PostMapping(value = "/save")
    @ApiOperation(value = "保存作业道路")
    public Result<JobRoadDTO> insertOrUpdate(@ApiIgnore @CurrentUser User user,
                                             @RequestBody @Validated JobRoadDetailParam jobRoadDetailParam,
                                             BindingResult bindingResult) throws BindException {
        if (bindingResult.hasErrors()) {
            throw new BindException(bindingResult);
        }
        return this.success(SysRespEnum.SUCCESS.getMessage(), jobRoadService.insertOrUpdate(user, jobRoadDetailParam));
    }
}

